/*
 * Copyright (c) 2023 huipei.x
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.xphsc.kubernetes.core.executor;

import cn.xphsc.kubernetes.core.query.NamespaceQuery;
import cn.xphsc.kubernetes.core.query.PodLogsQuery;
import cn.xphsc.kubernetes.core.query.ReadPodLogQuery;
import io.kubernetes.client.PodLogs;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.AppsV1Api;
import io.kubernetes.client.openapi.apis.BatchV1Api;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

/**
 * {@link }
 * @author <a href="xiongpeih@163.com">huipei.x</a>
 * @description: 用于获取Kubernetes中的所有pod Log Executor
 * @since 1.0.0
 */
public class PodLogExecutor<T> extends AbstractExecutor<T> {
    private String podLogsName;
    private  T entity;
    private PodLogs podLogs;
    private PodLogsQuery podLogsQuery;
    private  CoreV1Api  coreV1Api;
    private ReadPodLogQuery readPodLogQuery;
    public PodLogExecutor(ApiClient client, PodLogsQuery podLogsQuery, PodLogsMode podLogsMode) {
        super(client);
        this.podLogsName=podLogsMode.name();
        podLogs=new PodLogs(this.getClient());
        this.podLogsQuery=podLogsQuery;
    }

    public PodLogExecutor(ApiClient client, ReadPodLogQuery readPodLogQuery, PodLogsMode podLogsMode) {
        super(client);
        this.podLogsName=podLogsMode.name();
        this.coreV1Api=new CoreV1Api(this.getClient());
        this.readPodLogQuery=readPodLogQuery;
    }

    @Override
    protected  T doExecute() {
        if (StringUtils.isNotBlank(podLogsName)) {

            switch (podLogsName){
                case "DF":
                    try {
                        try {
                            entity= (T) podLogs.streamNamespacedPodLog(podLogsQuery.namespace(),podLogsQuery.name(),podLogsQuery.container());
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    } catch (ApiException e) {
                        throw new RuntimeException(e);
                    }
                    break;
                case "READ":
                    try {
                        entity = (T) coreV1Api.readNamespacedPodLog(readPodLogQuery.name(),readPodLogQuery.namespace(),readPodLogQuery.container(),readPodLogQuery.follow(),readPodLogQuery.insecureSkipTLSVerifyBackend(),readPodLogQuery.limitBytes(),readPodLogQuery.pretty(),readPodLogQuery.previous(),readPodLogQuery.sinceSeconds(),readPodLogQuery.tailLines(),readPodLogQuery.timestamps());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
            }

        }
        return (T) entity;
    }

    public static enum PodLogsMode {

        /**
         *
         */
        DF,
        READ,
        AUTO;

        private PodLogsMode() {

        }
        private PodLogsMode(String name) {
            this.name=name;
        }
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}


